package cn.tedu.travelsystem.transport.controller;

import cn.tedu.travelsystem.base.response.JsonResult;
import cn.tedu.travelsystem.base.response.StatusCode;
import cn.tedu.travelsystem.base.utils.AccessLimitUtil;
import cn.tedu.travelsystem.transport.mapper.TransportMapper;
import cn.tedu.travelsystem.transport.pojo.dto.TransportDTO;
import cn.tedu.travelsystem.transport.pojo.entity.Transport;
import cn.tedu.travelsystem.transport.pojo.vo.TransportVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/transports")
@Api(tags = "01.交通模块")
public class TransportController {

    @Autowired
    private TransportMapper transportMapper;

    @Autowired
    private AccessLimitUtil accessLimitUtil; // 引入访问限制工具类

    // 添加交通信息
    @PostMapping
    @ApiOperation("添加交通信息")
    @ApiOperationSupport(order = 10)
    public JsonResult addTransport(@Validated @RequestBody TransportDTO dto, HttpServletRequest request) {
        // 异常访问检测
        String ip = request.getRemoteAddr();
        String key = "limit:ip:" + ip;
        if (accessLimitUtil.isFrequent(key, 20, 60)) {
            // 发送警告信息
            log.warn("IP [{}] 频繁访问，60秒内请求超过限制", ip);
            return new JsonResult(StatusCode.REQUEST_TOO_FREQUENT);
        }

        log.debug("TransportDTO = " + dto);
        Transport transport = new Transport();
        BeanUtils.copyProperties(dto, transport);
        int rows = transportMapper.insert(transport);
        if (rows > 0) {
            return JsonResult.ok();
        } else {
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }
    }

    // 删除交通信息
    @DeleteMapping("/{transportId}")
    @ApiOperation("删除交通信息")
    @ApiOperationSupport(order = 20)
    public JsonResult deleteTransport(@PathVariable Integer transportId, HttpServletRequest request) {
        // 异常访问检测
        String ip = request.getRemoteAddr();
        String key = "limit:ip:" + ip;
        if (accessLimitUtil.isFrequent(key, 20, 60)) {
            // 发送警告信息
            log.warn("IP [{}] 频繁访问，60秒内请求超过限制", ip);
            return new JsonResult(StatusCode.REQUEST_TOO_FREQUENT);
        }

        log.debug("transportId = " + transportId);
        int rows = transportMapper.deleteById(transportId);
        if (rows > 0) {
            return JsonResult.ok();
        } else {
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }
    }

    // 更新交通信息
    @PutMapping("/{transportId}")
    @ApiOperation("更新交通信息")
    @ApiOperationSupport(order = 30)
    public JsonResult updateTransport(@PathVariable Integer transportId, @Validated @RequestBody TransportDTO dto, HttpServletRequest request) {
        // 异常访问检测
        String ip = request.getRemoteAddr();
        String key = "limit:ip:" + ip;
        if (accessLimitUtil.isFrequent(key, 20, 60)) {
            // 发送警告信息
            log.warn("IP [{}] 频繁访问，60秒内请求超过限制", ip);
            return new JsonResult(StatusCode.REQUEST_TOO_FREQUENT);
        }

        log.debug("transportId = " + transportId + ", TransportDTO = " + dto);
        Transport transport = new Transport();
        BeanUtils.copyProperties(dto, transport);
        transport.setTransport_id(transportId);
        int rows = transportMapper.update(transport);
        if (rows > 0) {
            return JsonResult.ok();
        } else {
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }
    }

    // 查询单个交通信息
    @GetMapping("/{transportId}")
    @ApiOperation("查询单个交通信息")
    @ApiOperationSupport(order = 40)
    public JsonResult getTransport(@PathVariable Integer transportId, HttpServletRequest request) {
        // 异常访问检测
        String ip = request.getRemoteAddr();
        String key = "limit:ip:" + ip;
        if (accessLimitUtil.isFrequent(key, 20, 60)) {
            // 发送警告信息
            log.warn("IP [{}] 频繁访问，60秒内请求超过限制", ip);
            return new JsonResult(StatusCode.REQUEST_TOO_FREQUENT);
        }

        log.debug("transportId = " + transportId);
        Transport transport = transportMapper.selectById(transportId);
        if (transport != null) {
            TransportVO vo = new TransportVO();
            BeanUtils.copyProperties(transport, vo);
            return JsonResult.ok(vo);
        } else {
            return new JsonResult(StatusCode.DATA_NOT_FOUND);
        }
    }

    // 查询所有交通信息
    @GetMapping
    @ApiOperation("查询所有交通信息")
    @ApiOperationSupport(order = 50)
    public JsonResult getAllTransports(HttpServletRequest request) {
        // 异常访问检测
        String ip = request.getRemoteAddr();
        String key = "limit:ip:" + ip;
        if (accessLimitUtil.isFrequent(key, 20, 60)) {
            // 发送警告信息
            log.warn("IP [{}] 频繁访问，60秒内请求超过限制", ip);
            return new JsonResult(StatusCode.REQUEST_TOO_FREQUENT);
        }

        log.debug("查询所有交通信息");
        List<Transport> transports = transportMapper.selectAll();
        List<TransportVO> vos = new ArrayList<>();
        for (Transport transport : transports) {
            TransportVO vo = new TransportVO();
            BeanUtils.copyProperties(transport, vo);
            vos.add(vo);
        }
        return JsonResult.ok(vos);
    }
}
